package com.example.project.controller.project;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.annotation.Log;
import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.core.page.TableDataInfo;
import com.example.project.entity.ProjectRole;
import com.example.project.enums.BusinessType;
import com.example.project.service.ProjectRoleService;

/**
 * 项目角色Controller
 * 
 * @author ruoyi
 * @date 2025-11-27
 */
@Controller
@RequestMapping("/project/role")
public class ProjectRoleController extends BaseController {

    private String prefix = "pm/project/role";

    @Autowired
    private ProjectRoleService projectRoleService;

    @RequiresPermissions("project:role:view")
    @GetMapping("/index")
    public String index() {
        return prefix + "/role";
    }

    @RequiresPermissions("project:role:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectRole projectRole) {
        startPage();
        List<ProjectRole> list = projectRoleService.selectProjectRoleList(projectRole);
        return getDataTable(list);
    }

    @RequiresPermissions("project:role:add")
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    @RequiresPermissions("project:role:add")
    @Log(title = "项目角色", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProjectRole projectRole) {
        projectRole.setCreateBy(getLoginName());
        return toAjax(projectRoleService.insertProjectRole(projectRole));
    }

    @RequiresPermissions("project:role:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProjectRole projectRole = projectRoleService.selectProjectRoleById(id);
        mmap.put("projectRole", projectRole);
        return prefix + "/edit";
    }

    @RequiresPermissions("project:role:edit")
    @Log(title = "项目角色", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectRole projectRole) {
        projectRole.setUpdateBy(getLoginName());
        return toAjax(projectRoleService.updateProjectRole(projectRole));
    }

    @RequiresPermissions("project:role:remove")
    @Log(title = "项目角色", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(projectRoleService.deleteProjectRoleByIds(ids));
    }

    @RequiresPermissions("project:role:view")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        ProjectRole projectRole = projectRoleService.selectProjectRoleById(id);
        mmap.put("projectRole", projectRole);
        return prefix + "/detail";
    }

    @RequiresPermissions("project:role:view")
    @GetMapping("/all")
    @ResponseBody
    public AjaxResult getAllRoles() {
        List<ProjectRole> roles = projectRoleService.selectProjectRoleList(new ProjectRole());
        return AjaxResult.success(roles);
    }
}